<!DOCTYPE html>
<html style="height: 100%">
<head>
    <meta charset="UTF-8">
    <title>SSL证书</title>
    <link rel="shortcut icon" href="/res/img/favicon.ico" type="image/x-icon">
    <link href="/res/layui/css/layui.css" rel="stylesheet">
    <link href="/res/css/common.css" rel="stylesheet">
</head>
<body class="layui-anim layui-anim-downbit">
<div class="layui-form flex-row" style="margin-bottom: 10px">
    <div class="layui-input-wrap" style="width: 80px">
        <button id="add" class="layui-btn layui-bg-blue"><i class="layui-icon layui-icon-add-1"></i>
            添加证书
        </button>
    </div>
    <div class="layui-input-wrap" style="width: 400px">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" placeholder="请输入要搜索的名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="flex-row" style="margin-left: 20px">
        <button class="layui-btn" lay-submit lay-filter="lay-search"><i class="layui-icon layui-icon-search"></i>搜索
        </button>
    </div>
</div>

<table id="tab" lay-filter="tab"></table>

<script src="/res/js/request.js"></script>
<script src="/res/layui/layui.js"></script>
<script type="text/html" id="barDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="see">详情</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
    </div>
</script>
<script>
    layui.use(['table', 'dropdown'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery

        // 创建渲染实例
        table.render({
            elem: '#tab',
            request: {
                pageName: 'page', // 页码的参数名称，默认：page
                limitName: 'size' // 每页数据条数的参数名，默认：limit
            },
            url: '/ssl/list', // 此处为静态模拟数据，实际使用时需换成真实接口
            parseData: function (res) { // res 即为原始返回的数据
                return {
                    "code": res.code, // 解析接口状态
                    "msg": res.msg, // 解析提示文本
                    "count": res.total, // 解析数据长度
                    "data": res.data // 解析数据列表
                };
            },
            height: 'full-70', // 最大高度减去其他容器已占有的高度差
            // cellMinWidth: 80,
            page: true,
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'name', fixed: 'left', width: 200, title: '名称', sort: true},
                {
                    field: 'domain', width: 180, title: '证书域名', sort: true, templet: function (r) {
                        var index = r.domain.indexOf('=')
                        if (index !== -1) {
                            return r.domain.substring(index + 1)
                        }
                        return r.domain
                    }
                },
                {field: 'remarks', width: 250, title: '备注'},
                {field: 'startTime', title: '签发时间', width: 170},
                {field: 'endTime', title: '失效时间', width: 170},
                {field: 'createTime', title: '创建时间', width: 170},
                {fixed: 'right', title: '操作', minWidth: 150, toolbar: '#barDemo'}
            ]]
        })
        // 触发单元格工具事件
        table.on('tool(tab)', function (obj) {
            var data = obj.data; // 获得当前行数据
            if (obj.event === 'see') {
                request.get('/ssl/get', {
                    data: {name: data.name},
                    success(res) {
                        layer.open({
                            type: 1,
                            offset: 'r',
                            title: '证书详情',
                            anim: 'slideLeft', // 从右往左
                            area: ['600px', '98%'],
                            shade: 0.2,
                            shadeClose: true,
                            id: 'ID-demo-2',
                            content: '<div class="flex-column layui-form" style="padding: 5px;">' +
                                '<h2>名称</h2>' +
                                '<p style="margin-top: 5px">' + res.data.name + '</p>' +
                                '<h2>创建时间</h2>' +
                                '<p style="margin-top: 5px">' + res.data.createTime + '</p>' +
                                '<h2>内容</h2>' +
                                '<textarea style="margin-top: 5px;height: 450px">' + (res.data ? JSON.stringify(res.data, null, 2) : '无') + '</textarea>' +
                                '</div>'
                        });
                    }
                })
            }
        });

        // 提交事件
        form.on('submit(lay-search)', function (data) {
            layer.msg('加载中', {icon: 16, shade: 0.3, time: 300});
            var field = data.field; // 获取表单字段值
            // 显示填写结果，仅作演示用
            // layer.alert(JSON.stringify(field), {
            //     title: '当前填写的字段值'
            // });
            table.reloadData('tab', {
                where: field, // 数据异步请求时携带的字段集 --- 属性设置有效，因属于数据相关属性
            });
        });

        $('#add').click(function () {
            layer.open({
                type: 1,
                offset: 'r',
                title: '添加证书',
                anim: 'slideLeft', // 从右往左
                area: ['500px', '100%'],
                shade: 0.2,
                shadeClose: true,
                id: 'ID-demo-layer-direction-r',
                content: `<div id="addssl" class="layui-form flex-column" style="padding: 16px;">
                            <div class="layui-form-item">
                                <label class="layui-form-label">名称</label>
                                <div class="layui-input-block">
                                  <input lay-verify="required" type="text" name="name" placeholder="名称" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                              <div class="layui-form-item layui-form-text">
                                <label class="layui-form-label">证书公钥</label>
                                <div class="layui-input-block">
                                  <textarea lay-verify="required" name="publicKey" placeholder="请输入公钥 fullchain.pem 内容" style="height: 200px" class="layui-textarea"></textarea>
                                </div>
                              </div>
                              <div class="layui-form-item layui-form-text">
                                <label class="layui-form-label">证书私钥</label>
                                <div class="layui-input-block">
                                  <textarea lay-verify="required" name="privateKey" placeholder="请输入私钥 privkey.pem 内容(PKCS#8)" style="height: 200px" class="layui-textarea"></textarea>
                                </div>
                              </div>
                              <div class="layui-form-item">
                                <label class="layui-form-label">密码</label>
                                <div class="layui-input-block">
                                  <input type="text" name="password" placeholder="钥密码，可为空" autocomplete="off" class="layui-input">
                                </div>
                              </div>
                          <div class="layui-form-item">
                            <div class="layui-input-block">
                              <button type="submit" class="layui-btn" lay-submit lay-filter="sub">立即提交</button>
                            </div>
                          </div>
                    </div>`,
                success: function (e, index, that) {
                    // 提交事件
                    form.on('submit(sub)', function (data) {
                        var field = data.field; // 获取表单字段值
                        var index_ = layer.msg('请等待', {icon: 16, shade: 0.6, time: 10000});
                        request.post('/ssl/addSSL', {
                            data: field,
                            success(res) {
                                layer.close(index);
                                layer.msg(res.msg, {icon: 1});
                                setTimeout(() => {
                                    window.location.reload()
                                }, 1000)
                            },
                            finally() {
                                setTimeout(function () {
                                    layer.close(index_);
                                }, 200)
                            }
                        })
                    });
                }
            });
        })
    })
</script>
</body>
</html>